package com.tianqu.tms.www.car.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsAjaxAction;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.entity.CommercialVehicle;
import com.tianqu.tms.entity.VehicleBrand;

/**
 * @author zhangxueliang
 * 车辆名称三级联动（第二级）
 */
public class CarLoadBrandInputAjaxAction extends TmsAjaxAction{

	private Logger logger = Logger.getLogger(CarLoadBrandInputAjaxAction.class);
	private static final String VENDORID = "vendorId";
	private static final String BRANDID="brandId";
	private static final String TYPE="type";
	@SuppressWarnings("unchecked")
	@Override
	public void process(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			UserContext uc)throws TmsException {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TxController controller = TxController.getTxController();
		controller.begin();
		String vendorId = request.getParameter(VENDORID);
		String brandId = request.getParameter(BRANDID);	
		String fromType = request.getParameter(TYPE);
		List list = new ArrayList();
		if("1".equals(fromType)){
			VehicleBrand brand = VehicleBrand.getInstance();
			list = brand.getAllVehicleBrandInfo(vendorId);
		}else if("2".equals(fromType)){
			CommercialVehicle commercialVehicle = CommercialVehicle.getInstance();
			list = commercialVehicle.getVehicleBrandInfo(vendorId);// 车主类型list
		}
				
		
		if(!StringUtil.isNullOrBlank(brandId)){
			//拼select
			StringBuffer str = new StringBuffer();
			str.append("<select name=\"carBrand\"  class=\"semcarchek requirecs\"  id=\"carBrand\" onchange=\"getCarStyleByAjax(this.value);\">");
			Map map = null;
			int j=0;
			for(int i=0;i<list.size();i++)
			{
				if(j==0){
					str.append("<option value=\""+""+"\">"+"车系"+"</option>");
				}
				map =(Map)list.get(i);
				//设置默认参数
				if(map.get("brandId").equals(brandId)){
					str.append("<option value='");
					str.append(map.get("brandId")+"'");
					str.append(" selected='");
					str.append("true'");
					str.append(" >");
					str.append(map.get("brandName"));
					str.append("</option>");
				
				}
				else{
				str.append("<option value='");
				str.append(map.get("brandId")+"'");
				str.append(" >");
				str.append(map.get("brandName"));
				str.append("</option>");
				}
				j++;
			}
			str.append("</select>");
			
			String responseText =str.toString();
			controller.commit();
			responseValues(response, responseText, TEXT);
		}else{
		//拼select
		StringBuffer str = new StringBuffer();
		str.append("<select name=\"carBrand\"  class=\"semcarchek requirecs\" id=\"carBrand\" onchange=\"getCarStyleByAjax(this.value);\">");
		Map map = null;
		int j=0;
		for(int i=0;i<list.size();i++)
		{
			if(j==0){
				str.append("<option value=\""+""+"\">"+"车系"+"</option>");
			}
			map =(Map)list.get(i);
			str.append("<option value=\""+map.get("brandId")+"\">"+map.get("brandName")+"</option>");
			j++;
		}
		str.append("</select>");
		
		String responseText =str.toString();
		controller.commit();
		responseValues(response, responseText, TEXT);
		}
		logger.debug(this.getClass().getName() + "#doAction was completed.");
		
	}

}
